package com.smbms.controller;

import com.smbms.pojo.Bill;
import com.smbms.service.BillService;
import com.smbms.util.BillPage;
import com.smbms.util.FileUtil;
import com.smbms.util.PageResult;
import com.smbms.util.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/bill")
public class BillController {
    @Autowired
    BillService billService;

    @ResponseBody
    @RequestMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable Integer page, @PathVariable Integer size, BillPage billPage) {
        //添加分页参数
        billPage.setPage(page);
        billPage.setSize(size);

        PageResult pageResult = billService.findPage(billPage);
        if(pageResult != null){
            return new Result(true, "查询成功", pageResult);
        }else {
            return new Result(false,"查询失败");
        }
    }

    @ResponseBody
    @RequestMapping("/addBill")
    public Result addBill(Bill bill){
        int count = billService.addBill(bill);
        if(count > 0){
            return new Result(true,"添加成功");
        }else {
            return new Result(false,"添加失败");
        }
    }

    @ResponseBody
    @RequestMapping("/checkByBillCode")
    public Result checkByBillCode(String billCode){
        Bill bill = billService.checkByBillCode(billCode);
        if(bill == null){
            return new Result(true,"该编码可以使用");
        }else {
            return new Result(false,"该编码已被占用");
        }
    }

    @ResponseBody
    @RequestMapping("/fileUpload")
    public Result fileUpload(MultipartFile file, HttpSession session) throws IOException {
        String fileName = FileUtil.upload(file, session);
        return new Result(true, "上传文件成功", fileName);
    }

    @ResponseBody
    @RequestMapping("/delBill")
    public Result delBill(Integer id){
        int count = billService.delBill(id);
        if(count > 0){
            return new Result(true,"删除成功");
        }else {
            return new Result(false,"删除失败");
        }
    }

    @ResponseBody
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Bill bill = billService.findById(id);
        if(bill != null){
            return new Result(true,"查询成功", bill);
        }else {
            return new Result(false,"查询失败", null);
        }
    }

    @ResponseBody
    @RequestMapping("/checkByBillCodeUpd")
    public Result checkByBillCodeUpd(String billCode, String self){
        int count = billService.checkByBillCodeUpd(billCode, self);
        if(count > 0){
            return new Result(false,"该编码已被占用");
        }else {
            return new Result(true,"该编码可以使用");
        }
    }

    @ResponseBody
    @RequestMapping("/updBill")
    public Result updBill(Bill bill){
        int count = billService.updBill(bill);
        if(count > 0){
            return new Result(true,"修改成功");
        }else {
            return new Result(false,"修改失败");
        }
    }

}
